package com.fs.system.college_admin.service;

import com.fs.system.college_admin.domain.Classes;
import com.fs.system.college_admin.domain.Speciality;
import com.fs.system.college_admin.mapper.CollegeAdminClassesMapper;
import com.fs.system.college_admin.mapper.CollegeAdminSpecialityMapper;
import com.fs.system.exception.CustomerException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class CollgeAdminClassesService {
    @Autowired
    private CollegeAdminClassesMapper collegeAdminClassesMapper;

    @Autowired
    private CollegeAdminSpecialityMapper collegeAdminSpecialityMapper;

    /**
     * 获取指定学院下的班级列表（优化版）:暂时用不到，不可用于分页查询
     * @param classes 查询条件（不含specialityId）
     * @param collegeId 学院ID
     * @return 班级列表
     */
    public List<Classes> getClassesByCollege(Classes classes, Long collegeId) {
        List<Classes> classesList = new ArrayList<>();
        Speciality speciality = new Speciality();
        speciality.setCollegeId(collegeId);
        List<Speciality> specialities = collegeAdminSpecialityMapper.selectAllSpecialityByCollege(speciality);

        for (Speciality dbSpeciality : specialities) {
            Classes queryClass = new Classes();
            // 复制查询条件
            queryClass.setName(classes.getName());
            queryClass.setDescription(classes.getDescription());
            // 设置当前专业ID
            queryClass.setSpecialityId(dbSpeciality.getSpecialityId());
            classesList.addAll(collegeAdminClassesMapper.getClasses(queryClass));
        }
        for(Classes cls:classesList){
            System.out.println("----------className:"+cls.getName());
        }
        return classesList;
    }

    /**
     * 分页查询（数据库层面分页）
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param classes 查询条件
     * @param collegeId 学院ID
     * @return 分页结果
     */
    public PageInfo<Classes> selectPage(Integer pageNum, Integer pageSize, Classes classes, Long collegeId) {
        // 设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        // 设置学院ID条件（确保SQL中使用该条件）
        if (classes == null) {
            classes = new Classes();
        }
        classes.setCollegeId(collegeId); // 确保SQL中使用该条件
        // 执行分页查询（数据库层面分页）
        List<Classes> classesList = collegeAdminClassesMapper.getClassesByCollegeId(classes);
        // 返回分页结果
        return new PageInfo<>(classesList);
    }

    /**
     * 添加班级（增强版重复检查）
     * @param classes 班级对象
     * @param collegeId 学院ID
     */
    public void addOneClasses(Classes classes, Long collegeId) {
        // 创建专门的查询对象，仅检查名称
        Classes nameCheckClass = new Classes();
        nameCheckClass.setName(classes.getName());

        // 获取学院下所有班级
        List<String> classesList = selectClassesByCollege(nameCheckClass, collegeId);

        // 检查同名班级
        for (String cls : classesList) {
            if (cls.equals(classes.getName())) {
                throw new CustomerException("500", "班级已存在");
            }
        }
        classes.setCreateTime(LocalDateTime.now());
        collegeAdminClassesMapper.addOneClasses(classes);
    }

    /**
     * 获取所有班级
     *
     * @param nameCheckClass
     * @return
     */
    private List<String> selectClassesByCollege(Classes nameCheckClass, Long collegeId) {
        Speciality speciality = new Speciality();
        speciality.setCollegeId(collegeId);
        List<Speciality> specialities = collegeAdminSpecialityMapper.selectAllSpecialityByCollege(speciality);
        List<String> className =new ArrayList<>();
        for (Speciality speciality1 : specialities){
            List<String> classList =collegeAdminClassesMapper.selectClassesBySpeciality(speciality1.getSpecialityId());
            className.addAll(classList);
        }
        return className;
    }

    /**
     * 修改班级信息
     * @param classes
     */
    public void updateClasses(Classes classes) {
        classes.setUpdateTime(LocalDateTime.now());
        collegeAdminClassesMapper.updateClasses(classes);
    }

    /**
     * 逻辑删除班级
     * @param classId
     * @param updateBy
     */
    public void deleteClasses(Long classId, String updateBy) {
        Classes classes = collegeAdminClassesMapper.selectById(classId);
        if(Objects.isNull( classes)){
            throw new CustomerException("500","删除失败");
        }
        classes.setUpdateBy(updateBy);
        classes.setDeleted(true);
        classes.setUpdateTime(LocalDateTime.now());
        collegeAdminClassesMapper.updateClasses(classes);
    }

    /**
     * 批量删除
     * @param updateBy
     * @param ids
     */
    public void deleteBatch(String updateBy, List<Long> ids) {
        for (Long classId :ids) {
            this.deleteClasses(classId, updateBy);
        }
    }
}